﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.IO;
using System.Drawing;

public partial class DanhMuc_DSDoanVienBiXoa : System.Web.UI.Page
{
    public readonly SecurityLib.Sec_AES _se = new SecurityLib.Sec_AES();
    public string st = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.BindGrid();
            st = _se.Decrypt(Request.QueryString["tid"].Replace(" ", "+"), "gold", 128);
        }
    }
 
    private void BindGrid()
    {
        _dbDataContext _db = new _dbDataContext();
        string donvi = _se.Decrypt(Request.QueryString["dvid"].Replace(" ", "+"), "gold", 128); //MemberShip_ASP.GetMaDonViFromUserName(Page.User.Identity.Name);
        var val = from p in _db.tblDoanViens.Where(r => r.TrangThaiXoa == true && r.TrucThuoc.Trim() == donvi)
                  select new MemberShip_ASP.QueryAll
                  {
                      MaDoanVien = p.MaDonVien.Trim(),
                      TenDoanVien = p.HoTen.Trim(),
                      Phai = (p.Phai == true ? "Nam" : "Nữ"),
                      NgaySinh = string.Format("{0:dd/MM/yyyy}", p.NgaySinh),
                      CMND = p.CMND.Trim(),
                      TrangThai=p.TrangThai
                  };
        Grid_DSDV_Xoa.DataSource = val;
        Grid_DSDV_Xoa.DataBind();
        
    }
  
   //xuat file excel
    protected void Btn_xuatfile_Click(object sender, EventArgs e)
    {
        Session.Remove("ListMaDoanVien_Print");
        Session.Remove("TenDonVi_Print");
        string listmadoanvien = "";
        CheckBox ckh;
        Label lbl;
        int count = 0;
        foreach (GridViewRow dr in Grid_DSDV_Xoa.Rows)
        {
            ckh = (CheckBox)dr.FindControl("chkchon");
            if (ckh.Checked == true)
            {
                lbl = (Label)dr.FindControl("lblMaDoanVien");
                listmadoanvien += lbl.Text.Trim()+"!";
                count++;
            }
        }
        if (count > 0)
        {
            Session["ListMaDoanVien_Print"] = listmadoanvien;
            Session["TenDonVi_Print"] = _se.Decrypt(Request.QueryString["tid"].Replace(" ", "+"), "gold", 128);
            Response.Redirect("javascript:window.open('../Print/PrintPage.aspx','_blank');");
        }
        else
        {
            SecurityLib.ToolsMore.ShowPopUpMsg("Chưa chọn đoàn viên để in.", this.Page);
        }
    }
    //xóa đoàn vĩnh viễn các đoàn viên dangd được chọn
    protected void Btn_xoadoanvien_Click(object sender, EventArgs e)
    {
        CheckBox select;
        string id = "";
        foreach (GridViewRow dr in Grid_DSDV_Xoa.Rows)
        {
            select = (CheckBox)dr.FindControl("chkchon");
            if (select.Checked == true)
            {
                id = Grid_DSDV_Xoa.DataKeys[dr.RowIndex].Value.ToString().Trim();
                CatalogAccess.XoaDoanVienVinhVien(id);
                CatalogAccess.Xoa_tt_truongthanhdoan(id);
                CatalogAccess.InsertHistoryDoanVien(id, MemberShip_ASP.GetDisplayName(this.Page.User.Identity.Name), DateTime.Now.ToString("dd/MM/yyyy"), "Xóa đoàn viên vĩnh viễn");
            }
        }
        BindGrid();
    }
    //khôi phục thông tin đoàn viên 
    protected void Btn_khoiphucdoanvien_Click(object sender, EventArgs e)
    {

        CheckBox select;
        string id = "";
        foreach (GridViewRow dr in Grid_DSDV_Xoa.Rows)
        {
            select = (CheckBox)dr.FindControl("chkchon");
            if (select.Checked == true)
            {
                id = Grid_DSDV_Xoa.DataKeys[dr.RowIndex].Value.ToString().Trim();
                CatalogAccess.Xoa_PhucHoi_DoanVien(id,false);
                CatalogAccess.InsertHistoryDoanVien(id, MemberShip_ASP.GetDisplayName(this.Page.User.Identity.Name), DateTime.Now.ToString("dd/MM/yyyy"), "Phục hồi trạng thái đoàn viên");
            }
        }
        BindGrid();
    }
    //xóa vĩnh viễn đoàn viên được chọn=checkbox
    protected void LinkButtonxoa_Click(object sender, EventArgs e)
    {
        CheckBox select;
        string id = "";
        foreach (GridViewRow dr in Grid_DSDV_Xoa.Rows)
        {
            select = (CheckBox)dr.FindControl("chkchon");
            if (select.Checked == true)
            {
                id = Grid_DSDV_Xoa.DataKeys[dr.RowIndex].Value.ToString().Trim();
                CatalogAccess.XoaDoanVienVinhVien(id);
                CatalogAccess.Xoa_tt_truongthanhdoan(id);
            }
        }
        BindGrid();
    }
    //xoa doan vien vinh vien:từng đoàn viên 1=trực tiếp trên gridview
    protected void Grid_DSDV_Xoa_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id =Grid_DSDV_Xoa.DataKeys[e.RowIndex].Value.ToString();
        if (CatalogAccess.XoaDoanVienVinhVien(id))
        {
            CatalogAccess.Xoa_tt_truongthanhdoan(id);
            CatalogAccess.InsertHistoryDoanVien(id, MemberShip_ASP.GetDisplayName(this.Page.User.Identity.Name), DateTime.Now.ToString("dd/MM/yyyy"), "Xóa đoàn viên vĩnh viễn");
        }
        else
        {
            Response.Write("<script>alert('Error!');</script>");
        }
        BindGrid();
    }
    //khoi phuc từng đoàn viên một=trực tiếp trên gridview
    protected void Grid_DSDV_Xoa_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string id = Grid_DSDV_Xoa.DataKeys[e.RowIndex].Value.ToString();
        if (CatalogAccess.Xoa_PhucHoi_DoanVien(id, false))
        {
            CatalogAccess.InsertHistoryDoanVien(id, MemberShip_ASP.GetDisplayName(this.Page.User.Identity.Name), DateTime.Now.ToString("dd/MM/yyyy"), "Phục hồi trạng thái đoàn viên");
            BindGrid();
        }
        else
        {
            Response.Write("<script>alert('Error!');</script>");
        }
    }
    //chọn all checkbox
    protected void chkInGridviewHeader_CheckedChanged(object sender, EventArgs e)//day la ham de goi trong header cua itemtemplate
    {
        CheckBox chk = (CheckBox)sender;
        CheckBox chkitem;
        foreach (GridViewRow dr in Grid_DSDV_Xoa.Rows)
        {
            chkitem = (CheckBox)dr.FindControl("chkchon");
            if (chk.Checked == true)
            {
                chkitem.Checked = true;
            }
            else
            {
                chkitem.Checked = false;
            }
        }
    }
    //Eport to Excel
    public void ExporttoExcel(object sender, EventArgs e)
    {
         
    }

    //thay doi mau cho tung loai trang thai
    protected void Grid_DSDV_Xoa_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        for (int i = 0; i <= Grid_DSDV_Xoa.Rows.Count - 1; i++)
        {
            Label lblparent = (Label)Grid_DSDV_Xoa.Rows[i].FindControl("lbltrangthai");

            if (lblparent.Text == "1")
            {
                Grid_DSDV_Xoa.Rows[i].Cells[10].Text = "<button class='btn btn-circle btn-success show-tooltip' data-original-title='Đoàn viên'><i class='icon-user'></i></button>";
            }
            else if(lblparent.Text == "3")
            {
                Grid_DSDV_Xoa.Rows[i].Cells[10].Text = "<button class='btn btn-circle  btn-primary show-tooltip' data-original-title='Đoàn viên trưởng thành'><i class= 'icon-star '></i></button>"; 
            }
            else if (lblparent.Text == "2")
            {
                Grid_DSDV_Xoa.Rows[i].Cells[10].Text = "<button class='btn btn-circle btn-warning  show-tooltip' data-original-title='Đoàn viên dự bị'><i class='icon-bookmark-empty'></i></button>";
            }
            else if (lblparent.Text == "5")
            {
                Grid_DSDV_Xoa.Rows[i].Cells[10].Text = "<button class='btn btn-circle btn-danger  show-tooltip' data-original-title='Đoàn viên bị khai trừ'><i class='icon-cut'></i></button>";
            }
        }
    }
    
    protected void btn_LocDoanVienKhaiTru_Click(object sender, EventArgs e)
    {
        VisibleRowInGrid("5");
    }
    protected void btn_LocDoanVienDuBi_Click(object sender, EventArgs e)
    {
        VisibleRowInGrid("2");
    }
    protected void btn_LocDoanVienTruongThanh_Click(object sender, EventArgs e)
    {
        VisibleRowInGrid("3");
    }
    protected void btn_LocDoanVien_Click(object sender, EventArgs e)
    {
        VisibleRowInGrid("1");
    }
    protected void btn_LocXemTatCa_Click(object sender, EventArgs e)
    {
        for (int i = 0; i <= Grid_DSDV_Xoa.Rows.Count - 1; i++)
        {
            Grid_DSDV_Xoa.Rows[i].Visible = true;
        }
    }
    //visible row in grid
    protected void VisibleRowInGrid(string trangthai)
    {
        for (int i = 0; i <= Grid_DSDV_Xoa.Rows.Count - 1; i++)
        {
            Label lblparent = (Label)Grid_DSDV_Xoa.Rows[i].FindControl("lbltrangthai");
            if (lblparent.Text != trangthai)
            {
                Grid_DSDV_Xoa.Rows[i].Visible = false;
            }
            else
            {
                Grid_DSDV_Xoa.Rows[i].Visible = true;
            }
        }
    }
}